// koa_router_demo.js

var Koa = require('koa');
var Router = require('@koa/router');

var app = new Koa();
var router = new Router();
const koaBody = require('koa-body');

router.get('/', (ctx, next) => {
    // ctx.router available
    console.log(ctx._matchedRoute)
    console.log(ctx._matchedRouteName)
    ctx.body = 'Hello World!'
});

router.get('user', '/users/:id', (ctx, next) => {
    // ...

    // console.log(ctx.req);
    // console.log('id: ', id);
    console.log(ctx.params);
    console.log('id: ', ctx.params.id);
    console.log(ctx.req._parsedUrl);
    console.log(ctx.req._parsedUrl.path);
    
    console.log(router.url('user', 3));
    ctx.body = 'user';
});

// router.url('user', 3);
// => "/users/3"

app.use(koaBody()); // (1)
app
    .use(router.routes())
    .use(router.allowedMethods());

app.listen(3000);

